package com.cjq.green.config;


import com.aliyun.green20220302.Client;
import com.aliyun.teaopenapi.models.Config;
import com.cjq.green.constant.EndPointConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@ConditionalOnClass(Client.class)
@EnableConfigurationProperties(GreenProperties.class)
public class GreenConf {

    @Resource
    private GreenProperties greenProperties;

    @Bean
    @ConditionalOnMissingBean
    public Client client() throws Exception {
        Config config = new Config();
        config.setAccessKeyId(greenProperties.getAccessKeyId());
        config.setAccessKeySecret(greenProperties.getAccessKeySecret());
        config.setReadTimeout(greenProperties.getReadTimeOut());
        config.setConnectTimeout(greenProperties.getConnectTimeOut());
        config.setRegionId(EndPointConstant.getEndPoint(greenProperties.getRegionId()).getRegion());
        config.setEndpoint(EndPointConstant.getEndPoint(greenProperties.getRegionId()).getEndPoint());
        return new Client(config);
    }

    @Bean
    @ConditionalOnMissingBean
    public Config config(){
        Config config = new Config();
        config.setAccessKeyId(greenProperties.getAccessKeyId());
        config.setAccessKeySecret(greenProperties.getAccessKeySecret());
        config.setReadTimeout(greenProperties.getReadTimeOut());
        config.setConnectTimeout(greenProperties.getConnectTimeOut());
        return config;
    }

}
